package previous.Test_0310;

import java.util.PriorityQueue;
import java.util.Stack;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //字符串解码
//    如果遇到数字，就一直进入
//    如果遇到‘[’如果为字符就将字符串都放入，如果为数字将数字都放入
//    如果遇到‘]’,将数字和字符串都拿出来，进行相乘，将之后得到的字符串与之前的相连
    public String decodeString(String s) {
        Stack<Integer> stack1=new Stack<>();
        Stack<StringBuilder> stack2=new Stack<>();
        stack2.add(new StringBuilder(""));
        char[] c=s.toCharArray();
        int len=c.length;
        int i=0;
       while(i<len){

            //注意是字符不是数字
            if(c[i]>='0' && c[i]<='9'){
                int tmp=0;
               while(i<len && c[i]>='0' && c[i]<='9' ){
                    tmp=tmp*10+(c[i]-'0');
                    i++;
               }
               stack1.add(tmp);
            } else if(c[i]=='['){
                StringBuilder stringBuilder=new StringBuilder();
                    while(i<len && c[i]>='a' && c[i]<='z'){
                         stringBuilder.append(c[i]);
                         i++;
                    }
                    stack2.add(stringBuilder);
                } else if(c[i]==']'){
                int num=stack1.pop();
                StringBuilder stringBuilder1=stack2.pop();
                while( num!=0) {
                    stack2.peek().append(stringBuilder1);
                    num--;
                }
                i++;
            }else{
                StringBuilder stringBuilder1=new StringBuilder();
                while(i<len && c[i]>='a' && c[i]<='z'){
                    stringBuilder1.append(c[i]);
                    i++;
                }
                stack2.peek().append(i);
            }
        }
        return stack2.pop().toString();
    }
    public class DecodeString {
        public String decodeString(String s) {
            Stack<Integer> stack1 = new Stack<>();
            Stack<StringBuilder> stack2 = new Stack<>();
            stack2.add(new StringBuilder(""));
            char[] c = s.toCharArray();
            int len = c.length;
            int i = 0;
            while (i < len) {
                if (c[i] >= '0' && c[i] <= '9') {
                    int tmp = 0;
                    while (i < len && c[i] >= '0' && c[i] <= '9') {
                        tmp = tmp * 10 + (c[i] - '0');
                        i++;
                    }
                    stack1.add(tmp);
                } else if (c[i] == '[') {
                    stack2.add(new StringBuilder());
                    i++;
                } else if (c[i] == ']') {
                    int num = stack1.pop();
                    StringBuilder stringBuilder1 = stack2.pop();
                    StringBuilder prev = stack2.peek();
                    for (int j = 0; j < num; j++) {
                        prev.append(stringBuilder1);
                    }
                    i++;
                } else {
                    StringBuilder stringBuilder1 = new StringBuilder();
                    while (i < len && (c[i] >= 'a' && c[i] <= 'z' )) {
                        stringBuilder1.append(c[i]);
                        i++;
                    }
                    stack2.peek().append(stringBuilder1);
                }
            }
            return stack2.pop().toString();
        }
    }
    //最后一块石头的重量
    public int lastStoneWeight(int[] stones) {
        //创建大跟堆
        PriorityQueue<Integer> q=new PriorityQueue<>((a, b) -> b-a);
        //放入
        for(int x:stones){
            q.offer(x);
        }
        while(q.size()>1){
            int a=q.poll();
            int b=q.poll();
            q.offer(a-b);
        }
        return q.isEmpty()? 0:q.peek();
    }

}
//数据流中第k大的元素
class KthLargest {
    PriorityQueue<Integer> q=new PriorityQueue();
    int k1;
    public KthLargest(int k, int[] nums) {
        k1=k;
        for(int x:nums){
            q.offer(x);
            while(q.size()>k){
                q.poll();
            }
        }
    }

    public int add(int val) {
        q.offer(val);
        while(q.size()>k1){
            q.poll();
        }
        return q.peek();
    }
}
